package com.sinothk.fc.storeSupplier.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.sinothk.fc.storeSupplier.entity.StoreSupplierEntity;
import com.sinothk.fc.storeSupplier.entity.StoreSupplierReq;
import com.sinothk.fc.storeSupplier.entity.StoreSupplierVO;
import com.sinothk.fc.storeSupplier.service.IStoreSupplierService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.jeecg.common.api.vo.Result;
import org.jeecg.common.aspect.annotation.AutoLog;
import org.jeecg.common.system.query.QueryGenerator;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;

/**
 * @Description: 门店供应商
 * @Author: Liangyt
 * @Date: 2022-10-29
 * @Version: V1.0
 */
@Api(tags = "门店供应商")
@RestController
@RequestMapping("/fc/storeSupplierCtrl")
@Slf4j
public class StoreSupplierController {

    private final IStoreSupplierService storeSupplierService;

    public StoreSupplierController(IStoreSupplierService storeSupplierService) {
        this.storeSupplierService = storeSupplierService;
    }

    /**
     * 添加更加供应商ID
     *
     * @param supplierId
     * @return
     */
    @AutoLog(value = "门店供应商-添加")
    @ApiOperation(value = "门店供应商-添加", notes = "门店供应商-添加")
    @GetMapping(value = "/supplierAdd")
    public Result<Boolean> supplierAdd(@RequestParam(name = "supplierId") String supplierId) throws Exception {
        return Result.OK(storeSupplierService.supplierAdd(supplierId));
    }

    /**
     * 门店供应商列表
     *
     * @param req
     * @param pageNo
     * @param pageSize
     * @return
     */
    @AutoLog(value = "门店供应商-门店供应商列表")
    @ApiOperation(value = "门店供应商-门店供应商列表", notes = "门店供应商-门店供应商列表")
    @GetMapping(value = "/storeSupplierPage")
    public Result<IPage<StoreSupplierVO>> storeSupplierPage(StoreSupplierReq req,
                                                            @RequestParam(name = "pageNo", defaultValue = "1") Integer pageNo,
                                                            @RequestParam(name = "pageSize", defaultValue = "10") Integer pageSize) throws Exception {
        IPage<StoreSupplierVO> pageList = storeSupplierService.storeSupplierPage(new Page<>(pageNo, pageSize), req);
        return Result.OK(pageList);
    }

    /**
     * 分页列表查询
     *
     * @param storeSupplier
     * @param pageNo
     * @param pageSize
     * @param req
     * @return
     */
    @AutoLog(value = "门店供应商-分页列表查询")
    @ApiOperation(value = "门店供应商-分页列表查询", notes = "门店供应商-分页列表查询")
    @GetMapping(value = "/list")
    public Result<?> queryPageList(StoreSupplierEntity storeSupplier,
                                   @RequestParam(name = "pageNo", defaultValue = "1") Integer pageNo,
                                   @RequestParam(name = "pageSize", defaultValue = "10") Integer pageSize,
                                   HttpServletRequest req) {
        QueryWrapper<StoreSupplierEntity> queryWrapper = QueryGenerator.initQueryWrapper(storeSupplier, req.getParameterMap());
        Page<StoreSupplierEntity> page = new Page<StoreSupplierEntity>(pageNo, pageSize);
        IPage<StoreSupplierEntity> pageList = storeSupplierService.page(page, queryWrapper);
        return Result.OK(pageList);
    }

    /**
     * 供应商-线下新增
     *
     * @param storeSupplier
     * @return
     */
    @AutoLog(value = "门店供应商-线下新增")
    @ApiOperation(value = "门店供应商-线下新增", notes = "门店供应商-线下新增")
    @PostMapping(value = "/supplierLocalAdd")
    public Result<?> supplierLocalAdd(@RequestBody StoreSupplierEntity storeSupplier) throws Exception {
        StoreSupplierEntity supplier = storeSupplierService.supplierLocalAdd(storeSupplier);
        return Result.OK(supplier);
    }

    /**
     * 添加
     *
     * @param storeSupplier
     * @return
     */
    @AutoLog(value = "门店供应商-添加")
    @ApiOperation(value = "门店供应商-添加", notes = "门店供应商-添加")
    @PostMapping(value = "/add")
    public Result<?> add(@RequestBody StoreSupplierEntity storeSupplier) {
        storeSupplierService.save(storeSupplier);
        return Result.OK("添加成功！");
    }

    /**
     * 编辑
     *
     * @param storeSupplier
     * @return
     */
    @AutoLog(value = "门店供应商-编辑")
    @ApiOperation(value = "门店供应商-编辑", notes = "门店供应商-编辑")
    @PutMapping(value = "/edit")
    public Result<?> edit(@RequestBody StoreSupplierEntity storeSupplier) {
        storeSupplierService.updateById(storeSupplier);
        return Result.OK("编辑成功!");
    }

    /**
     * 通过id删除
     *
     * @param id
     * @return
     */
    @AutoLog(value = "门店供应商-通过id删除")
    @ApiOperation(value = "门店供应商-通过id删除", notes = "门店供应商-通过id删除")
    @DeleteMapping(value = "/delete")
    public Result<?> delete(@RequestParam(name = "id", required = true) String id) {
        storeSupplierService.removeById(id);
        return Result.OK("删除成功!");
    }

    /**
     * 通过id查询
     *
     * @param id
     * @return
     */
    @AutoLog(value = "门店供应商-通过id查询")
    @ApiOperation(value = "门店供应商-通过id查询", notes = "门店供应商-通过id查询")
    @GetMapping(value = "/queryById")
    public Result<?> queryById(@RequestParam(name = "id", required = true) String id) {
        StoreSupplierEntity storeSupplier = storeSupplierService.getById(id);
        if (storeSupplier == null) {
            return Result.error("未找到对应数据");
        }
        return Result.OK(storeSupplier);
    }
}
